<template>
  <div class="dashboard-container">
    <div class="app-container">
      <UploadExcel :on-success="onSuccess" />
    </div>
  </div>
</template>

<script>
import { importEmployee } from '@/api/employees'
export default {
  methods: {
    async  onSuccess(data) {
      const headers = {
        '姓名': 'username',
        '手机号': 'mobile',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      const NewData = []
      await data.results.forEach(item => {
        const NewUser = {}
        for (const key in item) {
          const newKey = headers[key]
          newKey === 'timeOfEntry' || newKey === 'correctionTime' ? NewUser[newKey] = new Date(this.formatExcelTime(item[key])).toLocaleString() : NewUser[newKey] = item[key]
        }
        NewData.push(NewUser)
      })
      console.log(NewData)
      await importEmployee(NewData)
      this.$message.success('导入成功')
      this.$router.back()
    },
    formatExcelTime(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }

  }
}
</script>

<style>

</style>
